using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.GamerServices;
using ebookreader.Resources;

namespace ebookreader
{
    public class BookmarkSelPopUp : PopUp
    {
        #region Fields

        List<BoxedLabelScrollableButton> buttons = new List<BoxedLabelScrollableButton>();
        RenderableObject backgrounds, pop_up_box;
        //BoxedLabelButton back, go_to_page;
        VirtualKeyboard keyboard;
        TextureButton backspace;
        Reader reader;
                PopUp parent_popup;

        //SpriteFont font;
        #endregion
        State currentState;
        public BookmarkSelPopUp(Game game, Reader reader,PopUp parent_popup )
            ////: base(game, new Rectangle(392, 174, 194, 167))
            : base(game)
        {
            this.reader = reader;
            this.parent_popup = parent_popup;            
        }

        public override void Initialize()
        {
            sprite_batch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            input_manager.OnTap += new InputManager.Tap(OnTap);
            currentState = Game.Services.GetService(typeof(State)) as State;
            input_manager.onBackButtonPressed += new InputManager.Back(() => { this.Dispose(); });
            base.Initialize();
        }

        protected override void Dispose(bool disposing)
        {
            bookmarkList.Dispose();
            base.Dispose(disposing);
        }

        Vector2 dest = new Vector2(107, 431);
        Vector2 now = new Vector2(225, 351);
        public override void Draw(GameTime gameTime)
        {
            sprite_batch.Begin();

            //sprite_batch.Draw(popup_back, GraphicsDevice.Viewport.Bounds, Color.White);
            sprite_batch.Draw(popup_read_body, new Vector2(120, 80), Color.White);


            //sprite_batch.DrawString(font, Lang.Go_to_page, new Vector2(400 - font.MeasureString(Lang.Go_to_page).X, 131), Color.Black);

            //sprite_batch.DrawString(Game.Content.Load<SpriteFont>("Fonts/Arial"), "PROVA POPUP", new Vector2(300,200), Color.Green);
            sprite_batch.End();
            base.Draw(gameTime);

        }

        Texture2D popup_read_body, popup_back, button_ring, back, bookmark_el, long_button_ring, list_mask;
        SpriteFont font;
        BookmarkList bookmarkList;
        protected override void LoadContent()
        {
            popup_read_body = Game.Content.Load<Texture2D>("Definitivo/bookmark_popup_body");
            popup_back = Game.Content.Load<Texture2D>("todo/idle");
            button_ring = Game.Content.Load<Texture2D>("Definitivo/button_ring");
            long_button_ring = Game.Content.Load<Texture2D>("Definitivo/long_button_ring");
            back = Game.Content.Load<Texture2D>("Definitivo/black_back");
            //key = Game.Content.Load<Texture2D>("Definitivo/key");
            bookmark_el = Game.Content.Load<Texture2D>("Definitivo/bookmark_list_element");
           



            font = Game.Content.Load<SpriteFont>("Fonts/menu_piccolo");
           
            //input_manager.OnTap += new InputManager.Tap(OnTap);
            //ui_bookmark_box = Game.Content.Load<Texture2D>("Textures/UI/BookList/date_box");

            //ui_search_back = Game.Content.Load<Texture2D>("Textures/UI/search_back");
            //var ui_key = Game.Content.Load<Texture2D>("Textures/UI/key"); 
            //backgrounds = new RenderableObject();
            //pop_up_box = new RenderableObject();

            //back = new BoxedLabelButton();
            //go_to_page = new BoxedLabelButton();
            //backspace = new TextureButton();

            //entries.AddRange(new MenuEntry[] { back, go_to_page, backspace });
            bookmarkList = new BookmarkList(Game, reader, input_manager);
            Game.Components.Add(bookmarkList);

            var addBookmark = new BoxedLabelButton(sprite_batch, long_button_ring, new Vector2(146, 165), font, Lang.Add_bookmark, Color.Black, true, currentState.options);
            addBookmark.OnPress += new Press((m) =>
            {
                if (!Guide.IsVisible)
                    Guide.BeginShowKeyboardInput(
                       PlayerIndex.One,
                       Lang.Bookmark,
                       "",
                       "",
                       new AsyncCallback((result) =>
                       {
                           var text = Guide.EndShowKeyboardInput(result);
                           if (text!=null && !text.Equals(""))
                           {
                               var new_bokmark = new BookMark() { word = Reader.pages[reader.current_page].firstWord, name = text };
                               reader.currentState.currentBook.bookmarksList.Add(new_bokmark);
                               bookmarkList.addBookmarkButtonList(new_bokmark);
                           }

                       }),
                       null
                       );

               
            });
            entries.Add(addBookmark);



            var deleteBookmark = new BoxedLabelButton(sprite_batch, long_button_ring, new Vector2(146, 224), font, Lang.Delete_bookmark, Color.Black, true, currentState.options);
            deleteBookmark.OnPress += new Press((me) =>
                {
                    try
                    {
                        Game.Components.Add(new AlertPopup(Game, Lang.CancelBookmark + "\n" +
                            reader.currentState.currentBook.bookmarksList[bookmarkList.selectedBookmar].name,
                            () => { bookmarkList.removeBookmark(bookmarkList.selectedBookmar); }));
                    }
                    catch (Exception) { }
                });
            entries.Add(deleteBookmark);


            var confirm = new BoxedLabelButton(sprite_batch, button_ring, new Vector2(203, 282), font, Lang.MM_Continue, Color.Black, true, currentState.options);
            confirm.OnPress += new Press((me) =>
                {
                    if(bookmarkList.selectedBookmar < reader.currentState.currentBook.bookmarksList.Count)
                        reader.current_page = reader.findPage(reader.currentState.currentBook.bookmarksList[bookmarkList.selectedBookmar].word);
                    this.Dispose();
                    parent_popup.Dispose();
                });
            entries.Add(confirm);
            //});
            //entries.Add(addBookmark);


            //var addBookmark = new LabelButton(sprite_batch, font, new Vector2(500, 350), Color.Red, "AGGIUNGI");
            //addBookmark.OnPress += new Press((m) =>
            //{

            //    var vk = new VirtualKeyboard(
            //        Game, sprite_batch, font,
            //        new TextEdit(sprite_batch, font, ui_search_back, Color.Black, new Vector2(200, 100), "", 400, 60),
            //         ui_key, new string[] { "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h", "j", "k", "l", "z", "x", "c", "v", "b", "n", "m", "<-", "OK" }, 10, 10, 70, 250, 5, 5, 3,
            //         (s) =>
            //         {
            //             var new_bokmark = new BookMark() { word = reader.pages[reader.current_page].firstWord, name = s };
            //             reader.currentState.currentBook.bookmarksList.Add(new_bokmark);
            //             addBookmarkButtonList(new_bokmark, buttons.Count);
            //             //Game.Components.Remove(this);
            //             //this.Dispose();
            //         }, null);
                
            //});
            //entries.Add(addBookmark);

            //var close = new LabelButton(sprite_batch, font, new Vector2(500, 100), Color.Green, "Close");
            //close.OnPress += new Press((m) =>
            //{ this.Dispose(); });
            //entries.Add(close);


            var back_button = new TextureButton(sprite_batch, back, new Vector2(140, 100), currentState.options);
            back_button.OnPress += new Press(
                (me) => { this.Dispose(); }
                );
            entries.Add(back_button);

            base.LoadContent();
        }



        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }
    }
}
